package com.ruoyi.inventory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.Query;
import com.ruoyi.common.query.QueryTool;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.inventory.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.inventory.mapper.ProductCategoryMapper;
import com.ruoyi.inventory.domain.ProductCategory;
import com.ruoyi.inventory.service.IProductCategoryService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品类别Service业务层处理
 *
 * @author xwj
 * @date 2024-06-11
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements IProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private IProductService productService;

    /**
     * 查询商品类别
     *
     * @param id 商品类别主键
     * @return 商品类别
     */
    @Override
    public ProductCategory selectProductCategoryById(Long id) {
        return productCategoryMapper.selectProductCategoryById(id);
    }

    /**
     * 查询商品类别列表
     *
     * @param productCategory 商品类别
     * @return 商品类别
     */
    @Override
    public List<ProductCategory> selectProductCategoryList(ProductCategory productCategory) {
        return productCategoryMapper.selectProductCategoryList(productCategory);
    }

    /**
     * 新增商品类别
     *
     * @param productCategory 商品类别
     * @return 结果
     */
    @Override
    public ProductCategory insertProductCategory(ProductCategory productCategory) {
        setAncestors(productCategory);
        productCategory.setCreateTime(DateUtils.getNowDate());
        save(productCategory);
        return productCategory;
    }

    private void setAncestors(ProductCategory productCategory) {
        ProductCategory parent = getById(productCategory.getPid());
        if(parent != null) {
            String pancenstor = "";
            if(!StringUtils.isEmpty(parent.getAncestors())) {
                pancenstor = parent.getAncestors();
            }
            productCategory.setAncestors(pancenstor + "," + productCategory.getPid());
        } else {
            productCategory.setAncestors("");
        }
    }

    /**
     * 修改商品类别
     *
     * @param productCategory 商品类别
     * @return 结果
     */
    @Override
    public ProductCategory updateProductCategory(ProductCategory productCategory) {
        setAncestors(productCategory);
        productCategory.setUpdateTime(DateUtils.getNowDate());
        //  productCategoryMapper.updateProductCategory(productCategory);
        updateById(productCategory);
        return productCategory;
    }

    /**
     * 批量删除商品类别
     *
     * @param ids 需要删除的商品类别主键
     * @return 结果
     */
    @Override
    public int deleteProductCategoryByIds(Long[] ids) {
        return productCategoryMapper.deleteProductCategoryByIds(ids);
    }

    /**
     * 条件删除
     * @param productCategory 商品类别
     * @return
     */
    @Override
    @Transactional
    public int delete(ProductCategory productCategory) {
        List<ProductCategory> children = this.list("pid", productCategory.getId());

        if(children != null && children.size() > 0) {
            return -1;
        }

        QueryWrapper<ProductCategory> qw = new QueryWrapper<>();
        QueryTool.condition(qw, productCategory);
        ProductCategory exist = getOne(qw);

        int num = baseMapper.delete(qw);


        productService.setCategoryId(exist);


        return num;
    }

    /**
     * mp分页
     * @param productCategory 商品类别
     * @return
     */
    @Override
    public Pages<ProductCategory> queryPage(ProductCategory productCategory) {
        QueryWrapper<ProductCategory> qw = new QueryWrapper<>();
        QueryTool.condition(qw, productCategory);
        IPage<ProductCategory> ipage = new Query<ProductCategory>().getPage(productCategory);

        IPage<ProductCategory> page = this.page(
                ipage,
                qw
        );
        return new Pages<>(page);
    }

    /**
     * 删除商品类别信息
     *
     * @param id 商品类别主键
     * @return 结果
     */
    @Override
    public int deleteProductCategoryById(Long id) {
        return productCategoryMapper.deleteProductCategoryById(id);
    }
}
